﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;

using EHTWebsite.Code;
using EHTWebsiteProject.Code.BLL;
using System.Resources;
using System.Threading;
using System.Globalization;
using System.Text;
using EHTWebsiteProject.Code.Entity;

namespace EHTWebsiteProject.Administrator
{
    public partial class EHTModullAdd : System.Web.UI.Page
    {
        public Boolean IsVN = true;
        private String languageCode = "vi-VN";
        private DataTable dtProductCategorys = new DataTable();
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));
        private Int32 ModulID = -1;
        private String message = "";       

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                dtProductCategorys = DanhMucSanPhamProvider.GetDanhMucSanPhams(-1, "", "").Tables[0];

                try
                {
                    languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                    Session["NgonNgu"] = languageCode;
                    if (languageCode == "en-us")
                    {
                        IsVN = false;
                    }

                }
                catch (Exception ex)
                {
                }

                try
                {
                    ModulID = Int32.Parse(Request.QueryString["ModulID"].ToString());                    
                }
                catch
                {
                    ModulID = -1;
                }

                try
                {
                    message = Request.QueryString["Message"].ToString();
                    if (message != "")
                        ltrErorr.Text = HelpMethod.ApplySuccess(message);
                }
                catch
                {

                }

                if (ModulID != -1)
                {
                    plUpload.Visible = false;
                    if(IsVN==true)
                        lbUp_Edit.Text =HangSo.Edit_VN;
                    else
                        lbUp_Edit.Text = HangSo.Edit_Other;
                    Modul theModul = ModulProvider.GetModulByID(ModulID);
                    txtMenuNameOther.Text = theModul.TenModulOther;
                    txtMenuNameVN.Text = theModul.TenModul;
                    txtThuTu.Text = theModul.SapXep.ToString();
                    ddViTri.SelectedValue = theModul.Nhom.ToString();
                    ViewState["action"] = HangSo.ACTION_UPDATE;
                    ViewState["ModulID"] = ModulID;
                    ViewState["Url"] = theModul.Url;
                }
                else
                {
                    plUpload.Visible = true;
                    lbUp_Edit.Text = HangSo.Upload;
                    ViewState["action"] = HangSo.ACTION_ADD;
                }
                Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
                LoadLanguage(Thread.CurrentThread.CurrentCulture);
            }
        }

        public Boolean ValidateText()
        {
            Boolean flag = true;
            if (txtMenuNameOther.Text == "")
            {
                if (IsVN == true)
                    message = HangSo.check_VN;
                else
                    message = HangSo.check_Other;
                flag = false;
            }

            if (txtMenuNameVN.Text == "")
            {
                if (IsVN == true)
                    message = HangSo.check_VN;
                else
                    message = HangSo.check_Other;
                flag = false;
            }
            try
            {
                Int32.Parse(txtThuTu.Text);
            }
            catch
            {
                if (IsVN == true)
                    message = HangSo.messageNumBer;
                else
                    message = HangSo.messageNumBerOther;
                flag = false;
            }
            return flag;
            
        }

        /// <summary>
        /// Hàm kiểm tra file upload
        /// </summary>
        /// <returns></returns>
        public Boolean ValidateFile()
        {
            Boolean flag = true;
            string Aspx = ".aspx,.ascx,.ASPX,.ASCX";
            string Dll = ".dll,.DLL";
            string Pdb = ".pdb,.PDB";
            if (fulAspx.FileName == "")
            {
                if (IsVN == true)
                    message = HangSo.Choose_File;
                else
                    message = HangSo.Choose_FileOther;
                flag = false;
            }
            else if (this.fulAspx.HasFile && (Aspx.IndexOf(Path.GetExtension(fulAspx.PostedFile.FileName)) < 0))
            {
                if (IsVN == true)
                    message = HangSo.File;
                else
                    message = HangSo.FileOther;
                flag = false;
            }


            if (fulDll.FileName == "")
            {
                if (IsVN == true)
                    message = HangSo.Choose_File;
                else
                    message = HangSo.Choose_FileOther;
                flag = false;
            }
            else if (this.fulDll.HasFile && (Dll.IndexOf(Path.GetExtension(fulDll.PostedFile.FileName)) < 0))
            {
                if (IsVN == true)
                    message = HangSo.File;
                else
                    message = HangSo.FileOther;
                flag = false;
            }


            if (fulpdb.FileName == "")
            {
                if (IsVN == true)
                    message = HangSo.Choose_File;
                else
                    message = HangSo.Choose_FileOther;
                flag = false;
            }
            else if (this.fulpdb.HasFile && (Pdb.IndexOf(Path.GetExtension(fulpdb.PostedFile.FileName)) < 0))
            {
                if (IsVN == true)
                    message = HangSo.File;
                else
                    message = HangSo.FileOther;
                flag = false;
            }
            return flag;
        }

        /// <summary>
        /// Ham load ngon ngu
        /// </summary>
        /// <param name="ci"></param>
        public void LoadLanguage(CultureInfo ci)
        {            
            lbHelp.Text = rm.GetString("lbHelp", ci);
            lbSave.Text = rm.GetString("lbSave", ci);
            lbMenuNameVN.Text = rm.GetString("lbMenuNameVN", ci);
            lbMenuNameOther.Text = rm.GetString("lbMenuNameOther", ci);
            lbPhanVung.Text = rm.GetString("lbPhanVung", ci);
            lbThuTu.Text = rm.GetString("lbThuTu", ci);
            lbLoaiModul.Text = rm.GetString("lbLoaiModul", ci);
        }

        protected void ImgBat_Click(object sender, ImageClickEventArgs e)
        {
            String action = ViewState["action"].ToString();
            if (action == HangSo.ACTION_UPDATE)
            {
                if (ValidateText() == false)
                {
                    ltrErorr.Text = HelpMethod.ErorrLogin(message);
                    return;
                }
            }
            else
            {
                if (ValidateText() == false )
                {
                    ltrErorr.Text = HelpMethod.ErorrLogin(message);
                    return;
                }
                if (ValidateFile() == false)
                {
                    ltrErorr.Text = HelpMethod.ErorrLogin(message);
                    return;
                }
            }

            Modul theModul = new Modul();
            theModul.TenModul = txtMenuNameVN.Text;
            theModul.TenModulOther = txtMenuNameOther.Text;
            theModul.Nhom = ddViTri.SelectedValue;
            theModul.SapXep = Int32.Parse(txtThuTu.Text);
            if (action == HangSo.ACTION_UPDATE)
            {
                theModul.ModulID = Int32.Parse(ViewState["ModulID"].ToString());
                theModul.Url = ViewState["Url"].ToString();
                ModulProvider.IUModul(theModul, false);
            }
            else
            {
                theModul.Url = fulAspx.FileName.ToString();
                UploadFile();
                ModulProvider.IUModul(theModul, true);
            }
            Response.Redirect("EHTModul.aspx");
        }

        public void UploadFile()
        {
            if (radBack.Checked == true)
            {
                if (this.fulAspx.HasFile)
                {
                    String fileNameAspx = fulAspx.PostedFile.FileName;
                    try
                    {
                        FileInfo file = new FileInfo(Server.MapPath("..\\Administrator\\" + fileNameAspx));
                        if (file.Exists)
                        {
                            System.IO.File.Delete(Server.MapPath("~/Administrator/") + fileNameAspx);
                        }
                    }
                    catch
                    {
                    }
                    this.fulAspx.PostedFile.SaveAs(Server.MapPath("~/") + "Administrator//" + fileNameAspx);


                }
            }

            if (radFront.Checked == true)
            {
                if (this.fulAspx.HasFile)
                {
                    String fileNameAspx = fulAspx.PostedFile.FileName;
                    try
                    {
                        FileInfo file = new FileInfo(Server.MapPath("..\\ControlIndex\\" + fileNameAspx));
                        if (file.Exists)
                        {
                            System.IO.File.Delete(Server.MapPath("~/Index/ControlIndex/") + fileNameAspx);
                        }
                    }
                    catch
                    {
                    }
                    this.fulAspx.PostedFile.SaveAs(Server.MapPath("~/") + "Index//ControlIndex//" + fileNameAspx);


                }
            }
            if (this.fulDll.HasFile)
            {
                String fileNameDll = fulDll.PostedFile.FileName;
                try
                {
                    FileInfo file = new FileInfo(Server.MapPath("..\\bin\\" + fulDll));
                    if (file.Exists)
                    {
                        System.IO.File.Delete(Server.MapPath("~/bin/") + fulDll);
                    }
                }
                catch
                {
                }
                this.fulDll.PostedFile.SaveAs(Server.MapPath("~/") + "bin//" + fileNameDll); 
            }
            if (this.fulpdb.HasFile)
            {
                String fileNamePdb = fulpdb.PostedFile.FileName;
                try
                {
                    FileInfo file = new FileInfo(Server.MapPath("..\\bin\\" + fulpdb));
                    if (file.Exists)
                    {
                        System.IO.File.Delete(Server.MapPath("~/bin/") + fulpdb);
                    }
                }
                catch
                {
                }
                this.fulpdb.PostedFile.SaveAs(Server.MapPath("~/") + "bin//" + fileNamePdb); 
            }  
        }
    }
}